var tableElem = "LAY-home-feedback-list";
var formElem = "LAY-home-feedback-listform";
var tableToolsElem = "LAY-home-feedback-tools";
var formSubmitElem = "LAY-home-feedback-listform-submit";
var baseUrl = window.globalConfig.url_feedback


function showPic(pic) {
    layer.photos({
        photos: {
            "title": "创同盟",
            "start": 0,
            "data": [
                {
                    "alt": "创同盟",
                    "pid": 1,
                    "src": pic,
                }
            ]
        }
    });
}

layui.use(['common'], function (common) {
    var $ = layui.$
        , admin = layui.admin
        , table = layui.table
    //页面管理
    var tableItem = table.render({
        elem: `#${tableElem}`
        , url: baseUrl //模拟接口
        , lineStyle: "height:150px"
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'msg_id', width: 100, title: 'ID', sort: true}
            // , {field: 'nav_id', title: '导航id', minWidth: 100}
            , {field: 'user_company', title: '公司名称', minWidth: 100}
            , {field: 'user_name', title: '客户名', minWidth: 100}
            , {field: 'user_tel', title: '电话'}
            , {field: 'user_email', title: '邮箱'}
            , {field: 'msg_content', title: '留言信息'}
            , {
                field: 'msg_file', title: '留言文件', templet: (d) => {
                    let html = "无"
                    if (d.msg_file) {
                        html = `<img onclick="showPic('${window.globalConfig.base_domain + d.msg_file}')" src="${window.globalConfig.base_domain + d.msg_file}">`
                    }

                    return html;
                }
            }
            , {field: 'msg_time', title: '留言时间'}
            , {field: 'read_status', title: '是否已读', templet: "#ID-table-feedback-read_status-switch"}
            // , {field: 'url', title: '网页路由'}
            , {title: '操作', minWidth: 80, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: {none: '对不起，加载出现异常！'}
    });

    var active = {
        //添加
        export: function (othis) {
            /*
          var config = obj.config;
          var tableId = config.id;
          var status = treeTable.checkStatus(tableId);
          // 获取选中行
          if (obj.event === "getChecked") {
            if(!status.data.length) return layer.msg('无选中数据');
            console.log(status);
            layer.alert("当前数据选中已经输出到控制台，<br>您可按 F12 从控制台中查看结果。");
          }*/

            layer.confirm('确定导出留言？', function (index) {

                var status = table.checkStatus(tableElem)
                if (!status.data.length) return layer.msg('无选中数据');
                let datas = status.data;
                let ids = [];
                for (let i = 0; i < datas.length; i++) {
                    ids.push(datas[i].msg_id)
                }
                ids = ids.join(',');
                $url = baseUrl + "/create&access_token=" + layui.data('layuiAdmin').access_token + `&ids=${ids}`;
                window.open($url, 'blank')
                layer.close(index)
            });
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    // 增删改查
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') { // 删除页面
            layer.confirm('确定删除此反馈？', function (index) {
                admin.req({
                    url: baseUrl + "/" + data.msg_id,
                    type: "DELETE",
                    success: function (res) {
                        layer.msg(res.msg)
                        layer.close(index);
                        tableItem.reloadData(); //重载表格
                    }
                })
            });
        }
    });

    common.tableSwitchEditor('feedback-read_status-status', baseUrl)

});